<?php

class user_collectModel extends Model {
	const DYNAMIC_TYPE = 1;
	const GOODS_TYPE = 2;
	const DYNAMIC_TYPES = 3;

	public function isCollect($uid, $typeId, $type) {
		if ($info = $this->getOne('type=? and type_id=? and uid=?', [$type, $typeId, $uid])) {
			return 1;
		}
		return 0;
	}

	public function deleteByIds($uid, $type, $typeIds) {
		$typeIds = explode(',', $typeIds);
		if (!$typeIds) {
			return false;
		}
		$realIds = [];
		foreach ($typeIds as $typeId) {
			if ($typeId && is_numeric($typeId)) {
				$realIds[] = $typeId;
			}
		}
		$realIds = implode(',', $realIds);
		$sql = "delete from wf_user_collect where uid={$uid} and type={$type} and type_id in ({$realIds})";
		return $this->execute($sql);
	}

	public function getCollectIds($uid, $type, $typeIds) {
		$typeIds = explode(',', $typeIds);
		if (!$typeIds) {
			return [];
		}
		$realIds = [];
		foreach ($typeIds as $typeId) {
			if ($typeId && is_numeric($typeId)) {
				$realIds[] = $typeId;
			}
		}
		$ids = implode(',', $realIds);
		$sql = "select type_id from wf_user_collect where uid={$uid} and type={$type} and type_id in ({$ids})";
		$list = $this->query($sql)->fetchAll();
		$collectIds = [];
		if ($list) {
			foreach ($list as $row) {
				$collectIds[$row['type_id']] = true;
			}
		}
		$resultData = [];
		foreach ($realIds as $id) {
			$resultData[] = [
				'typeId' => intval($id),
				'isCollect' => isset($collectIds[$id]) ? 1 : 0,
			];
		}
		return $resultData;
	}

	public function getCount($uid, $type) {
		return $this->count('id', 'uid=? and type=?', [$uid, $type]);
	}

	public function getList($uid, $type, $page, $pageSize,$category_id = null) {
		$limit = ($page - 1) * $pageSize;
		$data = [
			'page' => $page,
			'pageSize' => $pageSize,
			'type' => intval($type),
			'list' => [],
		];
		$collectList = $this->fields('type_id')->order('id desc')->limit(($page - 1) * $pageSize, $pageSize)->getAll('uid=? and type=?', [$uid, $type]);
		if (!$collectList) {
			return $data;
		}
		$ids = [];
		foreach ($collectList as $row) {
			$ids[] = $row['type_id'];
		}
		$ids = implode(',', $ids);
		if ($type == self::DYNAMIC_TYPE || $type == 3) {
			$sql = "select d.*,u.nickname,u.avatar from wf_dynamic d left join wf_user u "
				. "on d.uid=u.uid "
				. "where d.dynamic_id in ({$ids})";

			$list = $this->query($sql)->fetchAll();
			if ($list) {
				foreach ($list as $info) {
					$data['list'][] = [
						'dynamicId' => intval($info['dynamic_id']),
						'title' => strval($info['title']),
						'image' => strval(ossModel::buildImageStyel($info['type'], $info['image'])),
						'gifImage' => strval($info['gif_image']),
						'type' => intval($info['type']),
						'collectTotal' => intval($info['collect_total']),
						'upTotal' => intval($info['up_total']),
						'userInfo' => [
							'uid' => intval($info['uid']),
							'nickname' => strval($info['nickname']),
							'avatar' => strval($info['avatar']),
						],
					];
				}
			}

		} elseif ($type == self::GOODS_TYPE) {
		    if(!empty($category_id)){
		        $category=$category_id;
            }else{
                $category=$this->categoryId();
            }
            $sql = "select g.goods_id,g.name,g.image,g.thumbnail,g.shop_id,g.sale_total,s.specification_id,s.original_price,s.sales_price,s.stock_total,g.postage,s.market_price from wf_goods g left join wf_goods_relate r on r.goods_id=g.goods_id left join wf_goods_specification s on s.goods_id=g.goods_id";
            $where=" where g.goods_id in ({$ids}) and s.type=2 and s.admin_id=0 and r.category_id in ({$category})";
            $sql = $sql.$where;
			$list = $this->query($sql)->fetchAll();
			if ($list) {
				foreach ($list as $goodsInfo) {
					$data['list'][] = [
						'goodsId' => intval($goodsInfo['goods_id']),
						'name' => strval($goodsInfo['name']),
						'price' => floatval($goodsInfo['sales_price']),
						'image' => strval($goodsInfo['image']),
						'saleTotal' => intval($goodsInfo['sale_total']),
						'stockTotal' => intval($goodsInfo['stock_total']),
					];
				}
			}
		}
		return $data;
	}

	private function categoryId(){
	    $category=self::model('goods_category')->getAll('parent_id=?',0);
	    $list=array();
	    foreach ($category as $v){
	        $list[]=$v['category_id'];
        }
        return implode(',',$list);

    }
}